<?php
//首页横幅控制器
class Controller_Admin_Appbanner extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'app_banner';
        parent::__construct($req, $res);

        $this->_list_where = array();

    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加横幅
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改横幅
	//$nid int 横幅id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            //$this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);
        }

        parent::action_update($nid);
    }

	//横幅详细信息
	//$nid int 横幅id
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        //$this->_form_data['iconUrl'] = explode('|', $data['iconUrl']);

        parent::action_detail($nid);
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
    	if(isset($post['iconUrl'])){
    	  foreach($post['iconUrl'] as $v){
            $file_name = DOCROOT.'/'.$v;
    		$image = Image::factory($file_name);
    		file_put_contents($file_name,$image->resize('300', '200', Image::NONE)->render());    		
    	  }
        }
        //$post['iconUrl'] = implode('|', $post['iconUrl']);
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
        	$rs['cityId']=App_City::getNameCnById($rs['cityId'], 'cityName');
        }
    }


}
